<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Controller_Membership extends Controller_Template {

  public function actionIndex() {
	// TODO: Выяснить что именно должно быть в начале страницы форма или просто таблица как в файле

    $this->addStyle('/styles/autoform.css');
    $this->template->title = "MEMBERSHIP APPLICATION-" . $this->settings['title'];
    $this->template->content = new View('membership/index');
    $content = Model::factory('directory')->getItemByToken('51x', 'text');
    $this->template->content->membershipContent = $content;
    $item = Model::factory('directory')->getItemByToken('z9b', 'membership');
    $this->template->content->item = $item;
    $image = Model::factory('directory')->getImage(51, 'membership');
    $this->template->content->image = $image;
    
    $attachments = Model::factory('directory')->getAttachmentsByItem('membership');
    $this->template->content->attachments = $attachments;
    $this->template->content->formTitle = 'Membership Dues and Application';
    $form = new Form('membership');
    $form->radio('ch1', array(
	'150' => '<b>BASIC BUSINESS</b>, 1-4 employees - $150/year',
	'200' => '<b>BASIC BUSINESS</b>, 5-9 employees - $200/year',
	'300' => '<b>BASIC BUSINESS</b>, 10+ employees - $300/year<br />',
	'302' => '<b>SILVER DONOR BUSINESS</b>, 1-4 employees - $300/year',
	'425' => '<b>SILVER DONOR BUSINESS</b>, 5-9 employees - $425/year',
	'500' => '<b>SILVER DONOR BUSINESS</b>, 10+ employees - $500/year<br />',
	'502' => '<b>GOLD DONOR BUSINESS</b>, 1-4 employees - $500/year',
	'750' => '<b>GOLD DONOR BUSINESS</b>, 5-9 employees - $750/year',
	'1000' => '<b>GOLD DONOR BUSINESS</b>, 10+ employees - $1000/year<br />',
	'1002' => '<b>PLATINUM DONOR BUSINESS</b>, 1-4 employees - $1000/year',
	'1500' => '<b>PLATINUM DONOR BUSINESS</b>, 5-9 employees - $1500/year',
	'2500' => '<b>PLATINUM DONOR BUSINESS</b>, 10+ employees - $2500/year<br />',
	'152' => '<b>RESIDENTIAL</b>, Local residents or property owners - $150/year'
	    ), '<b>Membership level</b>')->rule('required');
    $form->html('label2', '<b>Discounts</b>');
    $form->radio('ch15', array(
	'1' => '1st year business',
	'2' => 'small non-profits (1-4 employees)',
	'3' => 'any business also a member of a neighboring chamber of commerce'
	    ), '50% discount on Basic or Silver Business Membership Dues for:');
    $form->html('label3', '<b>Web Link</b>');
    $form->checkbox('ch14', '$25/year', 'Basic member can add a link to their website from chamber site www.westtownchamber.org');
    $form->html('label14', '<b>Member Business Information for Publication</b>');
    $form->text('name', 'Business Name')->attribute('size', 60)->rule('required');
    $form->text('address', 'Business Address (include zip code)')->attribute('size', 60)->rule('required');
    $form->text('onsitephone', 'Business Phone Number (include area code)')->attribute('size', 60)->rule('required');
    $form->text('onsitename', 'Website Address')->attribute('size', 60)->rule('url');
    $form->textarea('comment', 'Business Description (20 word or less)')->attribute('cols', 40)->attribute('rows', 5);
    $form->radio('category', array(
	'Retail' => 'Retail',
	'Restaraunt' => 'Restaraunt',
	'Bar/Club' => 'Bar/Club',
	'Restaraunt' => 'Professional Service',
	'Other' => 'Other'
	    ), 'Business Category')->rule('required');
    $form->text('hours', 'Business Hours')->attribute('size', 60);
    $form->html('label15', '<b>Member Contact Information (not for publication)</b>');
    $form->text('customer', 'Contact Name(s)')->attribute('size', 60)->rule('required');
    $form->text('email', 'E-mail Address(s)')->attribute('size', 60)->rule('required');
    $form->text('phone', 'Alternative Phone Number (Cell Phone)')->attribute('size', 60);
    $form->text('altmail', 'Alternative Mailing or Business Information')->attribute('size', 60);
    $form->hidden('indicate', '0');
    $form->submit('submit', 'Checkout');

    if (Request::$method == 'POST') {
      if (!isset($_POST['membership']['ch15'])) {
	$_POST['membership']['ch15'] = '';
	$_POST['membership']['ch16'] = '';
	$_POST['membership']['ch17'] = '';
	$_POST['membership']['ch18'] = '';
      }
      if ($form->validate()) {
	$values = $form->getValues();
	//print_r();
	switch ($values['ch1']) {
	  case '150' :
	    $values['ch1'] = 1;
	    $values['ch2'] = '';
	    $values['ch3'] = '';
	    $values['ch4'] = '';
	    $values['ch5'] = '';
	    $values['ch6'] = '';
	    $values['ch7'] = '';
	    $values['ch8'] = '';
	    $values['ch9'] = '';
	    $values['ch10'] = '';
	    $values['ch11'] = '';
	    $values['ch12'] = '';
	    $values['ch13'] = '';
	    break;
	  case '200' :
	    $values['ch1'] = '';
	    $values['ch2'] = 1;
	    $values['ch3'] = '';
	    $values['ch4'] = '';
	    $values['ch5'] = '';
	    $values['ch6'] = '';
	    $values['ch7'] = '';
	    $values['ch8'] = '';
	    $values['ch9'] = '';
	    $values['ch10'] = '';
	    $values['ch11'] = '';
	    $values['ch12'] = '';
	    $values['ch13'] = '';
	    break;
	  case '300' :
	    $values['ch1'] = '';
	    $values['ch2'] = '';
	    $values['ch3'] = 1;
	    $values['ch4'] = '';
	    $values['ch5'] = '';
	    $values['ch6'] = '';
	    $values['ch7'] = '';
	    $values['ch8'] = '';
	    $values['ch9'] = '';
	    $values['ch10'] = '';
	    $values['ch11'] = '';
	    $values['ch12'] = '';
	    $values['ch13'] = '';
	    break;
	  case '302' :
	    $values['ch1'] = '';
	    $values['ch2'] = '';
	    $values['ch3'] = '';
	    $values['ch4'] = 1;
	    $values['ch5'] = '';
	    $values['ch6'] = '';
	    $values['ch7'] = '';
	    $values['ch8'] = '';
	    $values['ch9'] = '';
	    $values['ch10'] = '';
	    $values['ch11'] = '';
	    $values['ch12'] = '';
	    $values['ch13'] = '';
	    break;
	  case '425' :
	    $values['ch1'] = '';
	    $values['ch2'] = '';
	    $values['ch3'] = '';
	    $values['ch4'] = '';
	    $values['ch5'] = 1;
	    $values['ch6'] = '';
	    $values['ch7'] = '';
	    $values['ch8'] = '';
	    $values['ch9'] = '';
	    $values['ch10'] = '';
	    $values['ch11'] = '';
	    $values['ch12'] = '';
	    $values['ch13'] = '';
	    break;
	  case '500' :
	    $values['ch1'] = '';
	    $values['ch2'] = '';
	    $values['ch3'] = '';
	    $values['ch4'] = '';
	    $values['ch5'] = '';
	    $values['ch6'] = 1;
	    $values['ch7'] = '';
	    $values['ch8'] = '';
	    $values['ch9'] = '';
	    $values['ch10'] = '';
	    $values['ch11'] = '';
	    $values['ch12'] = '';
	    $values['ch13'] = '';
	    break;
	  case '502' :
	    $values['ch1'] = '';
	    $values['ch2'] = '';
	    $values['ch3'] = '';
	    $values['ch4'] = '';
	    $values['ch5'] = '';
	    $values['ch6'] = '';
	    $values['ch7'] = 1;
	    $values['ch8'] = '';
	    $values['ch9'] = '';
	    $values['ch10'] = '';
	    $values['ch11'] = '';
	    $values['ch12'] = '';
	    $values['ch13'] = '';
	    break;
	  case '750' :
	    $values['ch1'] = '';
	    $values['ch2'] = '';
	    $values['ch3'] = '';
	    $values['ch4'] = '';
	    $values['ch5'] = '';
	    $values['ch6'] = '';
	    $values['ch7'] = '';
	    $values['ch8'] = 1;
	    $values['ch9'] = '';
	    $values['ch10'] = '';
	    $values['ch11'] = '';
	    $values['ch12'] = '';
	    $values['ch13'] = '';
	    break;
	  case '1000' :
	    $values['ch1'] = '';
	    $values['ch2'] = '';
	    $values['ch3'] = '';
	    $values['ch4'] = '';
	    $values['ch5'] = '';
	    $values['ch6'] = '';
	    $values['ch7'] = '';
	    $values['ch8'] = '';
	    $values['ch9'] = 1;
	    $values['ch10'] = '';
	    $values['ch11'] = '';
	    $values['ch12'] = '';
	    $values['ch13'] = '';
	    break;
	  case '1002' :
	    $values['ch1'] = '';
	    $values['ch2'] = '';
	    $values['ch3'] = '';
	    $values['ch4'] = '';
	    $values['ch5'] = '';
	    $values['ch6'] = '';
	    $values['ch7'] = '';
	    $values['ch8'] = '';
	    $values['ch9'] = '';
	    $values['ch10'] = 1;
	    $values['ch11'] = '';
	    $values['ch12'] = '';
	    $values['ch13'] = '';
	    break;
	  case '1500' :
	    $values['ch1'] = '';
	    $values['ch2'] = '';
	    $values['ch3'] = '';
	    $values['ch4'] = '';
	    $values['ch5'] = '';
	    $values['ch6'] = '';
	    $values['ch7'] = '';
	    $values['ch8'] = '';
	    $values['ch9'] = '';
	    $values['ch10'] = '';
	    $values['ch11'] = 1;
	    $values['ch12'] = '';
	    $values['ch13'] = '';
	    break;
	  case '2500' :
	    $values['ch1'] = '';
	    $values['ch2'] = '';
	    $values['ch3'] = '';
	    $values['ch4'] = '';
	    $values['ch5'] = '';
	    $values['ch6'] = '';
	    $values['ch7'] = '';
	    $values['ch8'] = '';
	    $values['ch9'] = '';
	    $values['ch10'] = '';
	    $values['ch11'] = '';
	    $values['ch12'] = 1;
	    $values['ch13'] = '';
	    break;
	  case '152' :
	    $values['ch1'] = '';
	    $values['ch2'] = '';
	    $values['ch3'] = '';
	    $values['ch4'] = '';
	    $values['ch5'] = '';
	    $values['ch6'] = '';
	    $values['ch7'] = '';
	    $values['ch8'] = '';
	    $values['ch9'] = '';
	    $values['ch10'] = '';
	    $values['ch11'] = '';
	    $values['ch12'] = '';
	    $values['ch13'] = 1;
	    break;
	  default :
	    $values['ch1'] = '';
	    $values['ch2'] = '';
	    $values['ch3'] = '';
	    $values['ch4'] = '';
	    $values['ch5'] = '';
	    $values['ch6'] = '';
	    $values['ch7'] = '';
	    $values['ch8'] = '';
	    $values['ch9'] = '';
	    $values['ch10'] = '';
	    $values['ch11'] = '';
	    $values['ch12'] = '';
	    $values['ch13'] = '';
	}

	switch ($values['ch15']) {
	  case 1 :
	    $values['ch15'] = 1;
	    $values['ch16'] = 1;
	    $values['ch17'] = '';
	    $values['ch18'] = '';
	    break;
	  case 2 :
	    $values['ch15'] = 1;
	    $values['ch16'] = '';
	    $values['ch17'] = 1;
	    $values['ch18'] = '';
	    break;
	  case 3 :
	    $values['ch15'] = 1;
	    $values['ch16'] = '';
	    $values['ch17'] = '';
	    $values['ch18'] = 1;
	    break;
	  default :
	    $values['ch15'] = '';
	    $values['ch16'] = '';
	    $values['ch17'] = '';
	    $values['ch18'] = '';
	}

	session_start();
	$j = 0;
	$buf = Array();
	if (isset($values['indicate'])) {
	  $_SESSION['indicate'] = $values['indicate'];
	}
	for ($i = 1; $i < 19; $i++) {
	  if ($values['ch' . $i]) {
	    $f = Model::factory('directory')->getItem($i + 88, 'cart');
	    $buf['cart'][$j] = $f;
	    $buf['cart'][$j]['source'] = 'directory';
	    $buf['cart'][$j]['quantity'] = 1;
	    $buf['cart'][$j]['discount'] = 0;
	    $values['ch' . $i] = 0;
	    if ($_POST['membership']['ch15'] != 0) {
	      if ($i == 15)
		$buf['cart'][$j]['discount'] = $_POST['membership']['ch15'];
	    }
	    //if($i==8) $buf['cart'][$j]['quantity'] = $_POST['qu2'];
	    $j++;
	  }
	}
//	print_r($values);
//	echo "<hr>";
	for($i=1; $i<19; $i++) {
	 unset($values['ch'.$i]);
	}
	unset($values['indicate']);
//	print_r($values);
//	exit();
	$_SESSION['cart'] = $buf;
	$_POST['cart'] = $buf;
	$_SESSION['customer'] = $values;
	$this->response->redirect('/commerce/checkout/');

	
      }
    }
    $this->template->content->form = $form;
  }

}
?>